package com.huatai.dataservice.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.dataservice.domain.TbFireResponseSpeed;
import com.huatai.dataservice.domain.TbFireSafetyHazardRecord;
import com.huatai.dataservice.domain.TbFireSafetySituation;
import com.huatai.dataservice.fire.firemodel.FireMonitoringWarningConstant;
import com.huatai.dataservice.fire.mapper.TbFireSafetyHazardRecordMapper;
import com.huatai.dataservice.fire.service.TbFireResponseSpeedService;
import com.huatai.dataservice.fire.service.TbFireSafetyHazardRecordService;
import com.huatai.dataservice.fire.service.TbFireSafetySituationService;
import com.huatai.dataservice.fire.mapper.TbFireSafetySituationMapper;
import com.huatai.dataservice.fire.firemodel.FireResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author htzy
 * @description 针对表【tb_fire_safety_situation(消防重点部位安全情况表)】的数据库操作Service实现
 * @createDate 2025-06-05 15:34:47
 */
@Service
public class TbFireSafetySituationServiceImpl extends ServiceImpl<TbFireSafetySituationMapper, TbFireSafetySituation>
        implements TbFireSafetySituationService {

    @Autowired
    private TbFireSafetySituationMapper tbFireSafetySituationMapper;

    @Autowired
    private TbFireSafetyHazardRecordMapper tbFireSafetyHazardRecordMapper;
    @Autowired
    private TbFireSafetyHazardRecordService tbFireSafetyHazardRecordService;

    /**
     * 获取消防重点部位安全情况
     *
     * @return
     * @author gaowengui
     */
    @Override
    public List<TbFireSafetySituation> selectFireSafetySituation() {
        List<TbFireSafetySituation> fireSafetySituationList = tbFireSafetySituationMapper.selectList(null);
        return fireSafetySituationList;
    }

    /**
     * 获取该重点部位的安全评分详情报告
     *
     * @param id
     * @return
     */
    @Override
    public FireResponseVo selectFireSafetySituationById(String id) {
        FireResponseVo fireResponseVo = new FireResponseVo();
        List<TbFireSafetySituation> tbFireSafetySituationList = null;
        List<TbFireSafetyHazardRecord> tbFireSafetyHazardRecordList = tbFireSafetyHazardRecordMapper.selectAll();
        if (id == null || id.isEmpty()) {
            tbFireSafetySituationList = tbFireSafetySituationMapper.selectAll();
        } else {
            //LambdaQueryWrapper<TbFireSafetyHazardRecord> queryWrapper = Wrappers.lambdaQuery();
            LambdaQueryWrapper<TbFireSafetySituation> queryWrapper1 = Wrappers.lambdaQuery();
            queryWrapper1.eq(TbFireSafetySituation::getId, id);
            tbFireSafetySituationList = tbFireSafetySituationMapper.selectList(queryWrapper1);
            //queryWrapper.eq(TbFireSafetyHazardRecord::getId,id);
            //tbFireSafetyHazardRecordList = tbFireSafetyHazardRecordMapper.selectList(queryWrapper);
        }
        ArrayList<Map<String, Object>> maps = new ArrayList<>();
        // 获取当前时间并格式化
        String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        if (tbFireSafetySituationList != null || tbFireSafetySituationList.isEmpty()) {
            for (TbFireSafetySituation tbFireSafetySituation : tbFireSafetySituationList) {
                String mc = tbFireSafetySituation.getMC();//unitName
                String ssqh = tbFireSafetySituation.getSSQH();//districName
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("comID", tbFireSafetySituation.getId());
                hashMap.put("loc", currentTime);
                hashMap.put("tmp", tbFireSafetySituation.getTemperature());
                hashMap.put("wind_dir", tbFireSafetySituation.getWindDir());
                hashMap.put("wind_sc", tbFireSafetySituation.getWindLevel());
                hashMap.put("hum", tbFireSafetySituation.getHumidity());
                hashMap.put("PM10", tbFireSafetySituation.getPm10());
                hashMap.put("PM25", tbFireSafetySituation.getPm25());
                List<Map<String, String>> dataList = new ArrayList<>();
                hashMap.put("waterAlarm", dataList);
                hashMap.put("electricAlarm", dataList);
                hashMap.put("hostAlarm", dataList);
                for (TbFireSafetyHazardRecord tbFireSafetyHazardRecord : tbFireSafetyHazardRecordList) {
                    String unitname = tbFireSafetyHazardRecord.getUnitname();
                    String districtname = tbFireSafetyHazardRecord.getDistrictname();
                    List<Map<String, String>> waterList = new ArrayList<>();
                    List<Map<String, String>> electricList = new ArrayList<>();
                    List<Map<String, String>> hostList = new ArrayList<>();

                    if (mc.equals(unitname) && ssqh.equals(districtname)) {
                        Map<String, String> map = new HashMap<>();
                        if ("用水隐患告警".equals(tbFireSafetyHazardRecord.getMajorclass())) {
                            map.put("AlarmType", tbFireSafetyHazardRecord.getSubclass());
                            map.put("AlarmTime", tbFireSafetyHazardRecord.getTime());
                            waterList.add(map);
                            hashMap.put("waterAlarm", waterList);
                        }else {
                            hashMap.put("waterAlarm", waterList);
                        }
                        if ("用电隐患告警".equals(tbFireSafetyHazardRecord.getMajorclass())) {
                            map.put("AlarmType", tbFireSafetyHazardRecord.getSubclass());
                            map.put("AlarmTime", tbFireSafetyHazardRecord.getTime());
                            electricList.add(map);
                            hashMap.put("electricAlarm", electricList);
                        }else {
                            hashMap.put("electricAlarm", electricList);
                        }
                        if ("主机告警".equals(tbFireSafetyHazardRecord.getMajorclass())) {
                            map.put("AlarmType", tbFireSafetyHazardRecord.getSubclass());
                            map.put("AlarmTime", tbFireSafetyHazardRecord.getTime());
                            hostList.add(map);
                            hashMap.put("hostAlarm", hostList);
                        }else {
                            hashMap.put("hostAlarm", hostList);
                        }
                    }
                }
                maps.add(hashMap);
            }
        }
        fireResponseVo.setCount(tbFireSafetySituationList.size());
        fireResponseVo.setData(maps);
        fireResponseVo.setMethodName(FireMonitoringWarningConstant.GET_SAFE_LIST_DETAIL);
        return fireResponseVo;
    }

    /**
     * 获取隐患告警总览统计数据
     *
     * @return
     * @author gaowengui
     */
    @Override
    public FireResponseVo selectFireSafetySituationAll() {
        FireResponseVo fireResponseVo = new FireResponseVo();
        List<TbFireSafetySituation> fireSafetySituationList = tbFireSafetySituationMapper.selectList(null);
        Map<String, Long> countMap = new HashMap<>();
        if (fireSafetySituationList != null && fireSafetySituationList.size() > 0) {
            long count = fireSafetySituationList.stream()
                    .filter(item -> Integer.parseInt(item.getAlarmcount()) == 0)
                    .count();
            countMap.put("superviseCount", (long) fireSafetySituationList.size());
            countMap.put("safetyCount", count);
        }
        fireResponseVo.setMethodName("获取安全情况总览");
        fireResponseVo.setCount(1);
        fireResponseVo.setData(countMap);
        return fireResponseVo;
    }
}




